package com.sugar.queryinfo.module.business.util;

import cn.hutool.json.JSONUtil;
import com.sugar.queryinfo.module.business.api.bi.dto.resp.ZCI007RespVO;
import com.sugar.queryinfo.module.business.api.bi.dto.resp.ZCI008RespVO;
import com.sugar.queryinfo.module.business.api.bi.dto.resp.ZCI022RespVO;
import com.sugar.queryinfo.module.business.dal.dataobject.dataqueryfyxq.DataQueryFyxqDO;
import com.sugar.queryinfo.module.business.dal.dataobject.dataqueryqjda.DataQueryQjdaDO;
import com.sugar.queryinfo.module.business.dal.dataobject.dataqueryqjld.DataQueryQjldDO;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class ObjConvertUtil {

    public static DataQueryQjldDO convertQjld(ZCI008RespVO zci008RespVO){
        DataQueryQjldDO dataQueryQjldDO = new DataQueryQjldDO();

        ZCI008RespVO.ApplyReportDetail applyReportDetail = zci008RespVO.getApply_report_detail();
        if (applyReportDetail != null) {
            dataQueryQjldDO.setA22160001(applyReportDetail.getA22160001())
                    .setA22160002(applyReportDetail.getA22160002())
                    .setA22160003(applyReportDetail.getA22160003())
                    .setA22160004(applyReportDetail.getA22160004())
                    .setA22160005(applyReportDetail.getA22160005())
                    .setA22160006(applyReportDetail.getA22160006())
                    .setA22160007(applyReportDetail.getA22160007())
                    .setA22160008(applyReportDetail.getA22160008())
                    .setA22160009(applyReportDetail.getA22160009())
                    .setA22160010(applyReportDetail.getA22160010());
        }
        ZCI008RespVO.CurrentReportDetail currentReportDetail = zci008RespVO.getCurrent_report_detail();
        if (currentReportDetail != null){
            dataQueryQjldDO.setC22180001(currentReportDetail.getC22180001())
                    .setC22180002(currentReportDetail.getC22180002())
                    .setC22180003(currentReportDetail.getC22180003())
                    .setC22180004(currentReportDetail.getC22180004())
                    .setC22180005(currentReportDetail.getC22180005())
                    .setC22180006(currentReportDetail.getC22180006())
                    .setC22180007(currentReportDetail.getC22180007())
                    .setC22180008(currentReportDetail.getC22180008())
                    .setC22180009(currentReportDetail.getC22180009())
                    .setC22180010(currentReportDetail.getC22180010());
        }
        ZCI008RespVO.BehaviorReportDetail behaviorReportDetail = zci008RespVO.getBehavior_report_detail();
        if (behaviorReportDetail != null){
            dataQueryQjldDO.setB22170001(behaviorReportDetail.getB22170001())
                    .setB22170002(behaviorReportDetail.getB22170002())
                    .setB22170003(behaviorReportDetail.getB22170003())
                    .setB22170004(behaviorReportDetail.getB22170004())
                    .setB22170005(behaviorReportDetail.getB22170005())
                    .setB22170006(behaviorReportDetail.getB22170006())
                    .setB22170007(behaviorReportDetail.getB22170007())
                    .setB22170008(behaviorReportDetail.getB22170008())
                    .setB22170009(behaviorReportDetail.getB22170009())
                    .setB22170010(behaviorReportDetail.getB22170010())
                    .setB22170011(behaviorReportDetail.getB22170011())
                    .setB22170012(behaviorReportDetail.getB22170012())
                    .setB22170013(behaviorReportDetail.getB22170013())
                    .setB22170014(behaviorReportDetail.getB22170014())
                    .setB22170015(behaviorReportDetail.getB22170015())
                    .setB22170016(behaviorReportDetail.getB22170016())
                    .setB22170017(behaviorReportDetail.getB22170017())
                    .setB22170018(behaviorReportDetail.getB22170018())
                    .setB22170019(behaviorReportDetail.getB22170019())
                    .setB22170020(behaviorReportDetail.getB22170020())
                    .setB22170021(behaviorReportDetail.getB22170021())
                    .setB22170022(behaviorReportDetail.getB22170022())
                    .setB22170023(behaviorReportDetail.getB22170023())
                    .setB22170024(behaviorReportDetail.getB22170024())
                    .setB22170025(behaviorReportDetail.getB22170025())
                    .setB22170026(behaviorReportDetail.getB22170026())
                    .setB22170027(behaviorReportDetail.getB22170027())
                    .setB22170028(behaviorReportDetail.getB22170028())
                    .setB22170029(behaviorReportDetail.getB22170029())
                    .setB22170030(behaviorReportDetail.getB22170030())
                    .setB22170031(behaviorReportDetail.getB22170031())
                    .setB22170032(behaviorReportDetail.getB22170032())
                    .setB22170033(behaviorReportDetail.getB22170033())
                    .setB22170034(behaviorReportDetail.getB22170034())
                    .setB22170035(behaviorReportDetail.getB22170035())
                    .setB22170036(behaviorReportDetail.getB22170036())
                    .setB22170037(behaviorReportDetail.getB22170037())
                    .setB22170038(behaviorReportDetail.getB22170038())
                    .setB22170039(behaviorReportDetail.getB22170039())
                    .setB22170040(behaviorReportDetail.getB22170040())
                    .setB22170041(behaviorReportDetail.getB22170041())
                    .setB22170042(behaviorReportDetail.getB22170042())
                    .setB22170043(behaviorReportDetail.getB22170043())
                    .setB22170044(behaviorReportDetail.getB22170044())
                    .setB22170045(behaviorReportDetail.getB22170045())
                    .setB22170046(behaviorReportDetail.getB22170046())
                    .setB22170047(behaviorReportDetail.getB22170047())
                    .setB22170048(behaviorReportDetail.getB22170048())
                    .setB22170049(behaviorReportDetail.getB22170049())
                    .setB22170050(behaviorReportDetail.getB22170050())
                    .setB22170051(behaviorReportDetail.getB22170051())
                    .setB22170052(behaviorReportDetail.getB22170052())
                    .setB22170053(behaviorReportDetail.getB22170053())
                    .setB22170054(behaviorReportDetail.getB22170054());
        }
        return dataQueryQjldDO;
    }

    public static DataQueryQjdaDO convertQjda(ZCI022RespVO qjdan){
        DataQueryQjdaDO dataQueryQjdaDO = new DataQueryQjdaDO();

        dataQueryQjdaDO.setMemberCount(qjdan.getMember_count())
                .setOrderCount(qjdan.getOrder_count())
                .setDebtAmount(qjdan.getDebt_amount())
                .setDebtDetail(qjdan.getDebt_detail())
                .setCurrentOrgCount(qjdan.getCurrent_org_count())
                .setCurrentOrderCount(qjdan.getCurrent_order_count())
                .setCurrentOrderAmt(qjdan.getCurrent_order_amt())
                .setCurrentOrderLendAmt(qjdan.getCurrent_order_lend_amt())
                .setTotaldebtDetail(qjdan.getTotaldebt_detail());

        return dataQueryQjdaDO;
    }

    public static DataQueryFyxqDO convertFyfx(List<ZCI007RespVO> fxfxList, String name, String idCard){
        DataQueryFyxqDO dataQueryFyxqDO = new DataQueryFyxqDO();
        // 目前只取执行公告
        List<ZCI007RespVO> finalResultList = fxfxList.stream().filter(f -> "zxgg".equals(f.getDataType())).collect(Collectors.toList());
        dataQueryFyxqDO.setAjfx(finalResultList);

        // 计算各种数值

        // 案件总数
        int ajzs = 0;
        // 已结案总数
        int yjazs = 0;
        // 未结案总数
        int wjazs = 0;
        // 被告总数
        int bgzs = 0;
        // 被告已结案总数
        int bgyjas = 0;
        // 被告未结案总数
        int bgwjas = 0;
        // 涉案总金额
        BigDecimal sazje = BigDecimal.ZERO;
        // 已结案金额
        BigDecimal yjaje = BigDecimal.ZERO;
        // 未结案金额
        BigDecimal wjaje = BigDecimal.ZERO;
        // 被告金额
        BigDecimal bgje = BigDecimal.ZERO;
        // 被告已结案金额
        BigDecimal bgyjaje = BigDecimal.ZERO;
        // 被告未结案金额
        BigDecimal bgwjaje = BigDecimal.ZERO;

        for (ZCI007RespVO data : finalResultList) {
            boolean isBg = idCard.equals(data.getIdcardNo()) || name.equals(data.getPname());

            BigDecimal amount;
            try {
                amount = new BigDecimal(data.getExecMoney());
            }catch (Throwable e){
                log.error("法院风险金额转换失败：", e);
                amount = BigDecimal.ZERO;
            }

            // 已结案
            if (done(data.getCaseState())){
                yjazs++;
                yjaje = yjaje.add(amount);

                if (isBg){
                    bgyjas++;

                    bgyjaje = bgyjaje.add(amount);
                }
            }else{ // 未结案
                wjazs++;
                wjaje = wjaje.add(amount);

                if (isBg){
                    bgwjas++;
                    bgwjaje = bgwjaje.add(amount);
                }
            }

            String caseState = data.getCaseState();
            if ("1".equals(caseState)){
                data.setCaseState("已结案");
            }else if ("0".equals(caseState)){
                data.setCaseState("执行中");
            }
        }

        ajzs = yjazs + wjazs;
        bgzs = bgwjas + bgyjas;

        sazje = yjaje.add(wjaje);
        bgje = bgyjaje.add(bgwjaje);

        dataQueryFyxqDO.setAjzs(String.valueOf(ajzs))
                .setYjazs(String.valueOf(yjazs))
                .setWjazs(String.valueOf(wjazs))
                .setBgzs(String.valueOf(bgzs))
                .setBgyjas(String.valueOf(bgyjas))
                .setBgwjas(String.valueOf(bgwjas))
                .setSazje(sazje.toString())
                .setYjaje(yjaje.toString())
                .setWjaje(wjaje.toString())
                .setBgje(bgje.toString())
                .setBgyjaje(bgyjaje.toString())
                .setBgwjaje(bgwjaje.toString());

        return dataQueryFyxqDO;
    }

    private static Boolean done(String caseState){
        return "1".equals(caseState) || "结案".equals(caseState) || "归档".equals(caseState)
                || "执行完毕".equals(caseState) || "报结".equals(caseState) || "已结案".equals(caseState);
    }
}
